package com.sad.jetpack.v1.datamodel.api.extension.client.socket.ipc;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.sad.jetpack.v1.datamodel.api.utils.AddressUtils;
import com.sad.jetpack.v1.datamodel.api.utils.LogcatUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class IPCServerAddress {
    public static final int SERVER_PORT=10276;
    private String ip="";
    private String id="";
    private int port=SERVER_PORT;

    public IPCServerAddress(@NonNull String id, @NonNull String ip, int port) {
        this.id=id;
        this.ip = ip;
        this.port = port;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String toJsonStr(){
        try {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",getId());
            jsonObject.put("ip",getIp());
            jsonObject.put("port",getPort());
            return jsonObject.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    public static IPCServerAddress parse(String jsonStr){
        try {
            JSONObject jsonObject=new JSONObject(jsonStr);
            return new IPCServerAddress(jsonObject.optString("id"),jsonObject.optString("ip"),jsonObject.optInt("port"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static void saveLocalAddress(Context context,String id,int port){
        try {
            String ip= AddressUtils.getLocalIP();
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",id);
            jsonObject.put("ip",ip);
            jsonObject.put("port",port);
            saveLocalAddress(context,jsonObject);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void saveLocalAddress(Context context, JSONObject jsonObject){
        SharedPreferences sharedPreferences=context.getSharedPreferences("SAD_IPC_LIGHT",Context.MODE_MULTI_PROCESS|Context.MODE_PRIVATE);
        sharedPreferences.edit().putString("server_address_"+jsonObject.optString("id"),jsonObject.toString()).commit();
        LogcatUtils.e("----------->存储当前IP信息:"+jsonObject);
    }

    public static void saveLocalAddress(Context context, IPCServerAddress address){
        try {
            saveLocalAddress(context,new JSONObject(address.toJsonStr()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static IPCServerAddress getServerAddress(Context context,String id){
        SharedPreferences sharedPreferences=context.getSharedPreferences("SAD_IPC_LIGHT",Context.MODE_MULTI_PROCESS|Context.MODE_PRIVATE);
        String s=sharedPreferences.getString("server_address_"+id,"");
        if (!TextUtils.isEmpty(s)){
            try {

                return IPCServerAddress.parse(s);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

}
